package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Override
    public void save(Dish dish) {
        dishMapper.insert(dish);
        List<DishFlavor> dishFlavors = dish.getFlavors();
        for (DishFlavor dishFlavor : dishFlavors) {
            dishFlavor.setDishId(dish.getId());
            dishFlavorMapper.insert(dishFlavor);
        }
    }

    @Override
    public List<Dish> findByCategoryId(Long categoryId,String name) {
        if (name==null && categoryId!=null) {
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getCategoryId, categoryId)
                    .eq(Dish::getStatus, 1);
            List<Dish> dishList = dishMapper.selectList(wrapper);
            if (CollectionUtil.isNotEmpty(dishList)) {
                for (Dish dish : dishList) {
                    LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                    wrapper1.eq(DishFlavor::getDishId, dish.getId());
                    List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                    dish.setFlavors(dishFlavors);
                }
            }
            return dishList;
        }else {
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(Dish::getName,name);
            List<Dish> dishList = dishMapper.selectList(wrapper);

            return dishList;
        }

    }
    @Override
    public void deleted(List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus()==0){
                dishMapper.deleteById(dish);
                LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(DishFlavor::getDishId,dish.getId());
                dishFlavorMapper.delete(wrapper);
            }else if (dish.getStatus()==1){
                throw new CustomException("此菜品处于销售状态，无法删除");
            }
        }
    }

    @Override
    public void findByStatus(Integer status, List<Long> ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            dish.setStatus(status);
            dishMapper.updateById(dish);
        }
    }

    @Override
    public void update(Dish dish) {
        dishMapper.updateById(dish);
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        dishFlavorMapper.delete(wrapper);
        List<DishFlavor> dishFlavors = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(dishFlavors)) {
            for (DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        Dish dish = dishMapper.selectById(id);
        Category category = categoryMapper.selectById(dish.getCategoryId());
        dish.setCategoryName(category.getName());
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavor = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavor);
        return dish;
    }

    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        Page<Dish> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        page = dishMapper.selectPage(page, wrapper);
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());
            }
        }
        return page;
    }
}
